package com.example.x6.configurationmaintenance.DataBase.TimeTick;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import com.example.x6.configurationmaintenance.Interface.OnNearestTimeChangeListener;
import com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDataBaseChangeListener;
import com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDatabaseErgodicListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class DatabaseOpration {
    private static DatabaseOpration databaseOpration;
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private DatabaseManager databaseManager;
    private OnDatabaseErgodicListener onDatabaseErgodicListener;
    private OnNearestTimeChangeListener onNearestTimeChangeListener;
    private final String pattern = "yyyy-MM-dd HH:mm:ss";

    public static synchronized void initializeInstance(Context context2) {
        synchronized (DatabaseOpration.class) {
            if (databaseOpration == null) {
                databaseOpration = new DatabaseOpration(context2);
            }
        }
    }

    public static synchronized DatabaseOpration getInstance() {
        DatabaseOpration databaseOpration2;
        synchronized (DatabaseOpration.class) {
            if (databaseOpration == null) {
                throw new IllegalStateException("DatabaseOperation is not initalized ,call initializeInstance(Context context) first");
            }
            databaseOpration2 = databaseOpration;
        }
        return databaseOpration2;
    }

    private DatabaseOpration(Context context2) {
        this.context = context2;
        DatabaseManager.initializelnstance(new DatabaseHelper(this.context));
        this.databaseManager = DatabaseManager.getInstance();
        this.databaseManager.setOnDataBaseChangeListener(new OnDataBaseChangeListener() {
            /* class com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDataBaseChangeListener
            public void OnDataBaseChange() {
                DatabaseOpration.this.getNearestTime();
            }
        });
    }

    public void setOnNearestTimeChangeListener(OnNearestTimeChangeListener onNearestTimeChangeListener2) {
        this.onNearestTimeChangeListener = onNearestTimeChangeListener2;
        getNearestTime();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private final long stringToLong(String stringTime) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(stringTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return -1;
        }
        return date.getTime();
    }

    public void getNearestTime() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration.AnonymousClass2 */

            public void run() {
                long nearestTime;
                String[] times = new String[0];
                Cursor cursor = DatabaseOpration.this.databaseManager.getReadableDatabase().rawQuery("select * from clock_reboot", times);
                boolean nearestIsOneShot = false;
                long currentTime = System.currentTimeMillis();
                long nearestTime2 = -2;
                String yyyy = new SimpleDateFormat("yyyy").format(Long.valueOf(currentTime));
                String MM = new SimpleDateFormat("MM").format(Long.valueOf(currentTime));
                String dd = new SimpleDateFormat("dd").format(Long.valueOf(currentTime));
                String nearestUuid = null;
                long[] taskTime = new long[cursor.getCount()];
                while (cursor.moveToNext()) {
                    nearestUuid = cursor.getString(cursor.getColumnIndex("uuid"));
                    String nearestHour = String.valueOf(cursor.getInt(cursor.getColumnIndex("clock_hour")));
                    String nearestMin = String.valueOf(cursor.getInt(cursor.getColumnIndex("clock_min")));
                    String nearestSec = String.valueOf(cursor.getInt(cursor.getColumnIndex("clock_second")));
                    boolean z = true;
                    if (cursor.getInt(cursor.getColumnIndex("is_one_shot")) != 1) {
                        z = false;
                    }
                    nearestIsOneShot = z;
                    DatabaseOpration databaseOpration = DatabaseOpration.this;
                    taskTime[cursor.getPosition()] = databaseOpration.stringToLong(yyyy + "-" + MM + "-" + dd + " " + nearestHour + ":" + nearestMin + ":" + nearestSec);
                    times = times;
                    nearestTime2 = nearestTime2;
                }
                Arrays.sort(taskTime);
                int i = 0;
                while (true) {
                    if (i >= taskTime.length) {
                        nearestTime = nearestTime2;
                        break;
                    } else if (taskTime[i] >= currentTime) {
                        nearestTime = taskTime[i];
                        break;
                    } else {
                        i++;
                    }
                }
                DatabaseOpration.this.onNearestTimeChangeListener.onNearestTimeChanged(nearestTime, nearestIsOneShot, nearestUuid);
                DatabaseOpration.this.databaseManager.closeDatabase();
            }
        }).start();
    }

    private boolean isHourValid(int hour) {
        if (hour < 0 || hour > 24) {
            return false;
        }
        return true;
    }

    private boolean isMinOrSecValid(int minOrSec) {
        if (minOrSec < 0 || minOrSec > 60) {
            return false;
        }
        return true;
    }

    private String getisOneShort(boolean isOneshort) {
        return isOneshort ? "1" : "0";
    }

    public synchronized boolean insert(int hour, int min, int sec, boolean isOneshort, String uuid) {
        if (isHourValid(hour) && isMinOrSecValid(min)) {
            if (isMinOrSecValid(sec)) {
                if (uuid != null) {
                    if (!uuid.isEmpty()) {
                        if (this.databaseManager.getReadableDatabase().rawQuery("select * from clock_reboot WHERE clock_hour='" + String.valueOf(hour) + "' AND clock_min='" + String.valueOf(min) + "' AND clock_second='" + String.valueOf(sec) + "'", new String[0]).getCount() > 0) {
                            this.databaseManager.closeDatabase();
                            return false;
                        }
                        this.databaseManager.getWritableDatabase().execSQL("insert into clock_reboot (uuid,clock_hour,clock_min,clock_second,is_one_shot) values('" + uuid + "','" + String.valueOf(hour) + "','" + String.valueOf(min) + "','" + String.valueOf(sec) + "','" + getisOneShort(isOneshort) + "')");
                        this.databaseManager.closeDatabase();
                        return true;
                    }
                }
                return false;
            }
        }
        return false;
    }

    public synchronized boolean delete(String uuid) {
        String str = this.TAG;
        Log.w(str, "remove " + uuid);
        if (uuid != null) {
            if (!uuid.isEmpty()) {
                this.databaseManager.getWritableDatabase().execSQL("delete from clock_reboot WHERE uuid='" + uuid + "'");
                this.databaseManager.closeDatabase();
                return true;
            }
        }
        return false;
    }

    public synchronized boolean delete(int hour, int min, int sec) {
        this.databaseManager.getWritableDatabase().execSQL("delete from clock_reboot WHERE clock_hour='" + String.valueOf(hour) + "' AND clock_min='" + String.valueOf(min) + "' AND clock_second='" + String.valueOf(sec) + "'");
        this.databaseManager.closeDatabase();
        return true;
    }

    public void setOnDatabaseErgodicListener(OnDatabaseErgodicListener onDatabaseErgodicListener2) {
        this.onDatabaseErgodicListener = onDatabaseErgodicListener2;
        startErgodic();
    }

    private void startErgodic() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration.AnonymousClass3 */

            public void run() {
                Cursor cursor = DatabaseOpration.this.databaseManager.getReadableDatabase().rawQuery("select * from clock_reboot", new String[0]);
                boolean isEmpty = true;
                while (cursor.moveToNext()) {
                    isEmpty = false;
                    DatabaseOpration.this.onDatabaseErgodicListener.databaseErgodic(cursor.getString(cursor.getColumnIndex("uuid")), cursor.getInt(cursor.getColumnIndex("clock_hour")), cursor.getInt(cursor.getColumnIndex("clock_min")), cursor.getInt(cursor.getColumnIndex("clock_second")), cursor.getInt(cursor.getColumnIndex("is_one_shot")) == 1);
                }
                DatabaseOpration.this.databaseManager.closeDatabase();
                DatabaseOpration.this.onDatabaseErgodicListener.databaseErgodicFinished(isEmpty);
            }
        }).start();
    }
}
